const approvalModel = require('../models/approvalModel');

// 创建审批
exports.createApproval = async (req, res) => {
    try {
        const id = await approvalModel.createApproval(req.body);
        res.json({ code: 200, message: '创建成功', id });
    } catch (err) {
        console.error(err);
        res.status(500).json({ code: 500, message: '服务器错误' });
    }
};

// 获取所有审批
exports.getAllApprovals = async (req, res) => {
    try {
        const data = await approvalModel.getAllApprovals();
        res.json({ code: 200, data });
    } catch (err) {
        console.error(err);
        res.status(500).json({ code: 500, message: '服务器错误' });
    }
};

// 获取审批详情
exports.getApprovalById = async (req, res) => {
    try {
        const data = await approvalModel.getApprovalById(req.params.id);
        if (!data) return res.status(404).json({ code: 404, message: '未找到该审批记录' });
        res.json({ code: 200, data });
    } catch (err) {
        console.error(err);
        res.status(500).json({ code: 500, message: '服务器错误' });
    }
};

// 更新审批
exports.updateApproval = async (req, res) => {
    try {
        const success = await approvalModel.updateApproval(req.params.id, req.body);
        if (!success) return res.status(404).json({ code: 404, message: '更新失败，记录不存在' });
        res.json({ code: 200, message: '更新成功' });
    } catch (err) {
        console.error(err);
        res.status(500).json({ code: 500, message: '服务器错误' });
    }
};

// 删除审批
exports.deleteApproval = async (req, res) => {
    try {
        const success = await approvalModel.deleteApproval(req.params.id);
        if (!success) return res.status(404).json({ code: 404, message: '删除失败，记录不存在' });
        res.json({ code: 200, message: '删除成功' });
    } catch (err) {
        console.error(err);
        res.status(500).json({ code: 500, message: '服务器错误' });
    }
};
